package asia.lxq.cic.entity;

import asia.lxq.cic.dao.AreaMapper;
import asia.lxq.cic.util.UserDirUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;


@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class User {

    @TableId(value = "user_id",type = IdType.AUTO)
    private int userId;
    private String username;
    private String password;
    private String nickname="张三";
    private Date createdDate;
    private String sex="保密";
    private String status;
    private String role;
    private String belong;
    private String description;
    private String number;
    private String email;

    @TableField(exist = false)
    String belongCollegeName;
    @TableField(exist = false)
    private int collegeId;
    @TableField(exist = false)
    private String createdDateFormatted;
    @TableField(exist = false)
    private String areaFormatted;
    @TableField(exist = false)
    private Group group;
    @TableField(exist = false)
    int groupId;
    @TableField(exist = false)
    String userPicPath;

    public void query(AreaMapper areaMapper){
        String[] split = belong.split(",");
        groupId=Integer.parseInt(split[3]);
        collegeId=Integer.parseInt(split[2]);
        File userPicFile=new File(UserDirUtil.getRealUserPic(userId));
        if (userPicFile.exists()) {
            userPicPath= UserDirUtil.getUserPic(userId);
        }
        if (collegeId!=-1){
            if (collegeId==0){
                belongCollegeName="全国";
            }else{
                belongCollegeName=areaMapper.selectCollegeByCollegeId(collegeId).collegeName;
            }
        }
        createdDateFormatted=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(createdDate);
        var province=areaMapper.selectOneArea(Integer.parseInt(split[0]));
        var city=areaMapper.selectOneArea(Integer.parseInt(split[1]));
        var className=group!=null?group.getGroupName():"未加入任何班级";
        areaFormatted=province+','+city+','+belongCollegeName+','+className;
    }

    public int getGroupId() {
        if (groupId!=0)return groupId;
        return Integer.parseInt(belong.split(",")[3]);
    }

    public int getCollegeId() {
        String[] split = belong.split(",");
        collegeId=Integer.parseInt(split[2]);
        return collegeId;
    }
}
